#include <iostream>
using namespace std;

int firstMissingPositive(int A[], int n) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    unsigned int mask = 0x80000000;
    int res = 0;

    for(int i=1; i<32; ++i){
        int zeroN = 0;
        for(int j=0; j<n; ++j){
            if(((A[j]-1) & mask) == (res & mask)){
                // pass test, pick out the i most significant bit
                int signBit = 0x1 & ((A[j]-1) >> (31-i));
                zeroN += signBit == 0? 1:0;
            }
        }
        int target = 0x1 << (31-i);
        if( zeroN >= target ){
            res |= (0x1 << (31-i));
        }
        mask |= 0x1 << (31-i);
    }

    return res+1;
}


int main(){

    int A[] = {1,3,7};
    cout<<firstMissingPositive(A,3)<<endl;
    return 0;
}

